﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Xml.Serialization;

namespace Rodunu.Movies.Services.IMDB
{
    public class IMDBController
    {
        //
        //http://cc00.clearspring.com/imdb/Search?Both=usual%20suspects

        private const string SEARCHIMDB = "http://cc00.clearspring.com/imdb/Search?Both=";
        private const string TITLELOCATION = "http://cc00.clearspring.com/imdb/LookupTitle?ResourceId=";

        public SearchResponse Search(string searchVariable)
        {
            var myRequest = (HttpWebRequest)WebRequest.Create(SEARCHIMDB + searchVariable);
            myRequest.Method = "GET";
            var myResponse = myRequest.GetResponse();

            var xmlSerializer = new XmlSerializer(typeof(SearchResponse));
            
            var searchResponse =
                (SearchResponse)
                xmlSerializer.Deserialize(new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8));
            return searchResponse;
        }

        public LookupTitleResponse GetMovie(string movieId)
        {
            var myRequest = (HttpWebRequest)WebRequest.Create(TITLELOCATION + movieId);
            myRequest.Method = "GET";
            var myResponse = myRequest.GetResponse();

            var xmlSerializer = new XmlSerializer(typeof(LookupTitleResponse));

            var searchResponse =
                (LookupTitleResponse)
                xmlSerializer.Deserialize(new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8));
            return searchResponse;
        }
    }
}
